package com.lanchetech.service.impl;


import com.lanchetech.common.enums.ExpTypeEnum;
import com.lanchetech.common.enums.HonorEnum;
import com.lanchetech.common.enums.MedalEnum;
import com.lanchetech.dao.ExpFlowMapper;
import com.lanchetech.dao.HonorMapper;
import com.lanchetech.dao.UserMapper;
import com.lanchetech.entity.ExpFlow;
import com.lanchetech.entity.Honor;
import com.lanchetech.entity.User;
import com.lanchetech.service.ExpService;
import com.lanchetech.service.HonorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;


@Service
public class HonorServiceImpl implements HonorService {

    @Autowired
    ExpFlowMapper expFlowMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    HonorMapper honorMapper;

    private void medalCheckAndInsert(User user, MedalEnum medalEnum) {
        Honor honor = honorMapper.findOneByUserIdAndContentAndType(user.getId(), medalEnum.getRemark(), HonorEnum.MEDAL.getType());

        if(honor == null){
            honorMapper.insert(Honor.builder()
                    .userId(user.getId())
                    .content(medalEnum.getRemark())
                    .remark(null)
                    .type(HonorEnum.MEDAL.getType())
                    .createdAt(new Date())
                    .build());
        }
    }

    @Override
    public void createMedalHonor(User user, Integer exp) {
        // 发放勋章
        if(exp >= 1000){
            medalCheckAndInsert(user, MedalEnum.FIVE);
        } else if (exp >= 500){
            medalCheckAndInsert(user, MedalEnum.FOUR);
        } else if (exp >= 200){
            medalCheckAndInsert(user, MedalEnum.THREE);
        } else if (exp >= 50){
            medalCheckAndInsert(user, MedalEnum.TWO);
        } else if (exp >= 10){
            medalCheckAndInsert(user, MedalEnum.ONE);
        }
    }
}